package cm.tedu._05mvcboot02.controller;

import cm.tedu._05mvcboot02.common.response.JsonResult;
import cm.tedu._05mvcboot02.common.response.StatusCode;
import cm.tedu._05mvcboot02.mapper.UserMapper;
import cm.tedu._05mvcboot02.pojo.dto.AddUserDTO;
import cm.tedu._05mvcboot02.pojo.entity.User;
import cm.tedu._05mvcboot02.pojo.vo.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * RestController:组合注解,相当于 ResponseBody注解 + Controller注解;
 *   1.加此注解后,类上无需再添加 Controller 注解;
 *   2.加此注解后,控制器方法上无需再添加 ResponseBody 注解.
 */
//@Controller
@Validated
@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired(required = false)
    private UserMapper userMapper;

    //添加用户
    @ApiOperation(value = "添加用户")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody @Validated AddUserDTO addUserDTO){
        log.debug("addUserDTO = " + addUserDTO);
        User user = new User();
        //插入数据:一定使用实体类
        //复制属性:addUserDTO ---->  user
        BeanUtils.copyProperties(addUserDTO, user);
        user.setCreated(new Date());
        userMapper.inserUser(user);
        return JsonResult.ok("添加成功");
    }

    //删除用户
    @ApiOperation(value = "删除用户")
    @GetMapping("delete")
    @ApiImplicitParam(name = "id", value = "用户编码", required = true, example = "100", dataType = "int")
    public JsonResult delete(@Range(min = 100, max = 200, message = "用户编码必须在100~200之间") @RequestParam int id){
        log.debug("id = " + id);
        if(id < 0){
            throw new IllegalArgumentException("ID值无效");
        }
        userMapper.deleteUser(id);
        return JsonResult.ok("删除成功");
    }

    //查询用户列表
    @ApiOperation(value = "查询用户列表")
    @GetMapping("userList")
    public JsonResult userList(){
        List<UserListVO> list = userMapper.selectUserList();
        if(list==null){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return JsonResult.ok(list);
    }

    //更新用户
    @ApiOperation(value = "更新用户")
    @PostMapping("update")
    public JsonResult update(User user){
        log.debug("user = " + user);
        userMapper.updateUser(user);
        return JsonResult.ok("修改成功");
    }

    //查询指定用户
    @ApiOperation(value = "查询指定用户")
    @GetMapping("selectById/{id}")
    @ApiImplicitParam(name = "id", value = "用户编码", required = true, dataType = "int", example = "1")
    public JsonResult selectById(@PathVariable int id){
        log.debug("id = " + id);
        User user = userMapper.selectUserById(id);
        return JsonResult.ok(user);
    }
}
